package com.zy.view;

import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

//分组动画View
public class SubSetTipsView extends View {
    public SubSetTipsView(Context context) {
        super(context);
    }

    public SubSetTipsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SubSetTipsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SubSetTipsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private float yBaseLine = 0;


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        yBaseLine = getHeight();

        pathPaintMap.forEach(new BiConsumer<Path, Paint>() {
            @Override
            public void accept(Path path, Paint paint) {
                canvas.drawPath(path, paint);
            }
        });

        pathPaintMap.clear();
    }

    Map<Path, Paint> pathPaintMap = new HashMap<>();

    //x轴坐标点 list
    public static class SubSetNode {
        List<Integer> xList;
        int height;
        int color;

        public SubSetNode(List<Integer> xList, int height, int color) {
            this.xList = xList;
            this.height = height;
            this.color = color;
        }
    }

    private List<SubSetNode> nodeList = new ArrayList<>();

    public void clear() {
        nodeList.clear();
    }


    public void addNodeList(List<SubSetNode> nodeList) {
        this.nodeList.addAll(nodeList);
    }


    public void addNode(SubSetNode node) {
        nodeList.add(node);
    }

    public void setYBaseLine(float yBaseLine) {
        this.yBaseLine = yBaseLine;
    }

    public void startAnimator(AnimatorListenerAdapter animatorListenerAdapter) {
        ValueAnimator mAnimator = ValueAnimator.ofFloat(0, 1);
        //mAnimator.setRepeatCount(-1);
        //mAnimator.setRepeatCount(1);
        mAnimator.setDuration(500);
        mAnimator.setInterpolator(new LinearInterpolator());
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //时间流,必须是线性的
                float current = (float) animation.getAnimatedValue();
                //updateBall();//更新小球信息
                paintPerAni(current);
                //invalidate();
            }
        });
        mAnimator.addListener(animatorListenerAdapter);
        mAnimator.start();
    }

    private void paintPerAni(float percentage) {
        for (SubSetNode node : nodeList) {
            buildStepPath(node.xList, node.height, node.color, percentage);
        }
        //转化为动画
        invalidate();
    }

    public void buildStepPath(List<Integer> xList, int height, int color, float percentage) {
        //根据参数设置Path
        if (xList.size() < 2) {
            return;
        }
        Path path = new Path();
        int length = xList.size();
        int lastX = xList.get(0);
        for (int i = 1; i < length; i++) {
            int curX = xList.get(i);
            //[lastX -  curX]

            path.moveTo(lastX, yBaseLine);
            //y变动
            path.lineTo(lastX, yBaseLine - height * percentage);
            //x变动
            if (percentage > 0.98) {
                path.lineTo(curX, yBaseLine - height * percentage);
            }
            lastX = curX;
        }

        //y变动
        path.moveTo(lastX, yBaseLine);
        path.lineTo(lastX, yBaseLine - height * percentage);

        Paint paint = new Paint();
        paint.setColor(color);
        paint.setStrokeWidth(8);
        paint.setStyle(Paint.Style.STROKE);
        pathPaintMap.put(path, paint);
    }
}
